package jiyun.com.yiyanmeng;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import base.BaseObserver;
import bean.PenBean;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import utils.ProgressDialogs;
import utils.RxUtil;
import utils.ToastUtil;

public class PenActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 取消
     */
    private TextView mTvLeft;
    /**
     * 返回
     */
    private TextView mImageBack;
    /**  */
    private TextView mTvTitle;
    private TextView mTvRight;
    private ImageView mImageRight;
    private RelativeLayout mRl;
    private FrameLayout mRlMain;
    /**
     * 请输入标题
     */
    private EditText mEtTitle;
    /**
     * 分享新鲜事...
     */
    private EditText mEtContent;
    private GridView mGvImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pen);
        initView();
        initData();
    }

    private void initData() {
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("发送");
        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = mEtTitle.getText().toString();
                if (TextUtils.isEmpty(title)){
                    ToastUtil.show(PenActivity.this,"请输入标题");
                    return;
                }
                String content = mEtContent.getText().toString();
                if (TextUtils.isEmpty(content)){
                    ToastUtil.show(PenActivity.this,"请输入内容");
                    return;
                }
                submit(title,content);
            }
        });

    }

    private void submit(String title, String content) {
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL, MySerivce.class);
        serivce.penbean(title,content).compose(RxUtil.<PenBean>changeThread())
                .subscribe(new BaseObserver <PenBean>() {
                    @Override
                    public void onSuccess(PenBean penBean) {
                        if (penBean.getRet().equals("200")){
                            ToastUtil.show(PenActivity.this,penBean.getMas());
                            finish();
                        }else{
                            ToastUtil.show(PenActivity.this,penBean.getMas());
                        }
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
    }

    private void initView() {
        mTvLeft = (TextView) findViewById(R.id.tv_left);
        mImageBack = (TextView) findViewById(R.id.image_back);
        mImageBack.setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvRight = (TextView) findViewById(R.id.tv_right);
        mTvRight.setOnClickListener(this);
        mRl = (RelativeLayout) findViewById(R.id.rl);
        mRlMain = (FrameLayout) findViewById(R.id.rl_main);
        mEtTitle = (EditText) findViewById(R.id.et_title);
        mEtTitle.setOnClickListener(this);
        mEtContent = (EditText) findViewById(R.id.et_content);
        mEtContent.setOnClickListener(this);
        mGvImage = (GridView) findViewById(R.id.gv_image);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.image_back:
                finish();
                break;
            case R.id.tv_right:
                break;
            case R.id.et_title:
                break;
            case R.id.et_content:
                break;
            case R.id.gv_image:
                break;
        }
    }
}
